// JavaScript Document
	/*鼠标滚轮事件*/
	function getWheelValue(e){e=e||event;return (e.wheelDelta?e.wheelDelta/120:-(e.detail%3==0?e.detail/3:e.detail));}
	function stopEvent(e){e=e||event;if(e.preventDefault)e.preventDefault();e.returnValue=false;}
	function addEvent(obj,type,fn){var isFirefox=typeof document.body.style.MozUserSelect != 'undefined';if(obj.addEventListener) obj.addEventListener(isFirefox ? 'DOMMouseScroll' : type,fn,false); else obj.attachEvent('on'+type,fn); return fn; }
	function delEvent(obj,type,fn){var isFirefox=typeof document.body.style.MozUserSelect != 'undefined';if(obj.removeEventListener)obj.removeEventListener(isFirefox?'DOMMouseScroll':type,fn,false);else obj.detachEvent('on'+type,fn);}
	/*[鼠标滚轮事件]*/

	function mousePosition(ev){
		if(ev.pageX || ev.pageY){
			return {x:ev.pageX, y:ev.pageY};
		}
		return {
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop  - document.body.clientTop
		};
	}
	
	document.onmousemove = mouseMove;
	mousePos=track0=track1={x:0,y:0};
	DragMaskOpa=0;
	tmrHideDragMask=null;
	bBlockStart=false;
	
	function mouseMove(ev){
		ev = ev || window.event;
		mousePos = mousePosition(ev);
		H$("DragMask").style.top=mousePos.y+"px";
		H$("DragMask").style.left=mousePos.x+"px";
		
		if(track0.x==0 && track0.y==0) return;
		//Animation
		
		tarImg="";
		if(Math.abs(track0.x-mousePos.x)<30){
			if(track0.y-mousePos.y>30)
				tarImg='ActionUp';
			else if(track0.y-mousePos.y<-30)
				tarImg='ActionDown';
			else
				hideAllDirections();
			DragMaskOpa=(Math.abs(track0.y-mousePos.y)-30)*(30-Math.abs(track0.x-mousePos.x))/40;
		}else if(Math.abs(track0.y-mousePos.y)<30){
			if(track0.x-mousePos.x>30)
				tarImg='ActionTop';
			else if(track0.x-mousePos.x<-30)
				tarImg='ActionBot';
			else
				hideAllDirections();
			DragMaskOpa=(Math.abs(track0.x-mousePos.x)-30)*(30-Math.abs(track0.y-mousePos.y))/40;
		}else{
			hideAllDirections();
		}
		if(tarImg!=""){
			Opacity(H$("DragMask"),DragMaskOpa);
			if(H$("DragMask").style.background!='url("'+H$(tarImg).src+'") no-repeat'){
				H$("DragMask").style.background='url("'+H$(tarImg).src+'") no-repeat';
			}
		}
		if(Math.abs(track0.x-mousePos.x)>5||Math.abs(track0.y-mousePos.y)>5) 
			SelectBlock();
	}
	
	function SelectBlockAttr(obj,b){
		if(b){
			try{obj.style.WebkitUserSelect="none";}catch(e){}
			try{obj.style.MozUserSelect="none";}catch(e){}
			try{obj.unselectable="on";}catch(e){}
			try{obj.setAttribute("unselectable","on",0);}catch(e){}
			try{obj.attachEvent('onselectstart',rtnFalse);}catch(e){}
		}else{
			try{obj.style.WebkitUserSelect="";}catch(e){}
			try{obj.style.MozUserSelect="";}catch(e){}
			try{obj.unselectable="off";}catch(e){}
			try{obj.setAttribute("unselectable","off",0);}catch(e){}
			try{obj.detachEvent('onselectstart',rtnFalse);}catch(e){}
		}
	}
	
	function SelectBlock(){
		if(bBlockStart) return;
		bBlockStart=true;
		SelectBlockAttr(H$("Displayer"),true);
		H$("DragMask").style.visibility="";
	}
	
	function SelectUnBlock(){
		if(!bBlockStart) return;
		bBlockStart=false;
		SelectBlockAttr(H$("Displayer"),false);
		H$("DragMask").style.visibility="hidden";
	}	
	
	function hideAllDirections(){
		H$("DragMask").style.background="";
	}
	
	function hideDragMask(){
		DragMaskOpa=DragMaskOpa-10;
		if(DragMaskOpa<1){
			DragMaskOpa=0;
			tmrHideDragMask=null;
			H$("DragMask").style.visibility="hidden";
		}else{
			tmrHideDragMask=window.setTimeout(hideDragMask,10);	
		}
		Opacity(H$("DragMask"),DragMaskOpa);
	}
	
	function rtnFalse(){return false;}
	
	function beginTrack(){
		SelectUnBlock();
		track0=mousePos;
		Opacity(H$("DragMask"),0);
		if(tmrHideDragMask!=null) window.clearTimeout(tmrHideDragMask);
		DragMaskOpa=0;
		hideAllDirections();
	}
	
	function endTrack(){
		SelectUnBlock();
		hideDragMask();
		var tmpTrack=track0;
		track0=track1={x:0,y:0};
		try{
			H$("Displayer").detachEvent('onselectstart',rtnFalse);
		}catch(e){}
		if(SlideCount==0&&(tmpTrack.x!=0 || tmpTrack.y!=0) ) {
			track1=mousePos;
			if(Math.abs(tmpTrack.x-track1.x)<30){
				if(tmpTrack.y-track1.y>30)
					getMode=('before');
				else if(tmpTrack.y-track1.y<-30)
					getMode=('after');
				else
					return;
			}else if(Math.abs(tmpTrack.y-track1.y)<30){
				if(tmpTrack.x-track1.x>30)
					getMode='real';
				else if(tmpTrack.x-track1.x<-30)
					getMode='base';
				else
					return;
			}else{
				return;
			}
			if(tmrFetchInterval!=0) window.clearTimeout(tmrFetchInterval);
			GetTop5();
		}
	}
	
	/*全局变量*/
	opaDLM=0;
	tmrFade=0;
	var xmlhttp = CreateXMLHttp();
	lgStat="";
	httpFailedTry=0;
	getMode="";
	tmrFetchInterval=0;
	
	opaItem=0;
	lastOrder=0;
	SlideCount=0;
	SlideHTMLSet=new Array();
	SlideDirection=0;
	
	opaTB=0;
	
	tmrFadeMusicPlayer=0;
	opaMP=0;
	flagFlashMusicPlayer=false;
	
	window.addEvent("domready",Window_Load);
	
	function Window_Load(){
		try{
			H$("divContent").innerHTML="<iframe id='fraContent' frameBorder='no' border='0' allowTransparency='true' src='about:blank' style='position:relative; width:490px; left:1%; top:0px; height:100%; border-width:0px; z-index:1992' scrolling='no'> </iframe>";
			H$("W3CValidate").innerHTML="<a href='http://validator.w3.org/check?uri=referer' target='_blank'><img src='http://www.w3.org/Icons/valid-xhtml10' alt='Valid XHTML 1.0 Transitional' height='31' width='88' style='border:none;' /></a>";
		}catch(e){
			window.alert("Error Loading Main Component(s). \nPlease check the network and refresh the page.");
		}
		Opacity(H$("LoginNotify"),0);
		ToolBarAnime();
		//fadeIn();
		GetTop5();
		Opacity(H$("Displayer"),60);
		H$("Displayer").style.visibility="";
		InitOrder();
		Opacity(H$("MusicPlayer"),0);
		Opacity(H$("Footer"),60);
		SelectBlockAttr(H$("MusicPlayer"),true);
		SelectBlockAttr(H$("Footer"),true);
		SelectBlockAttr(H$("W3CValidate"),true);
		//SelectBlockAttr(H$(""),true);
		PlayBgMusic();
		addEvent(H$('Displayer'),'mousewheel',function(e) 
		{
			if(SlideCount!=0) return;
			stopEvent(e);
			var delta = getWheelValue(e);
			if(delta>0)
				getMode='before';
			else
				getMode='after';
			if(tmrFetchInterval!=0) window.clearTimeout(tmrFetchInterval);
			GetTop5();
		});
		window.cacheImages();
	}

	function fadeInMusicPlayer(){
		if(tmrFadeMusicPlayer!=0){
			window.clearTimeout(tmrFadeMusicPlayer);
			tmrFadeMusicPlayer=0;
		}
		opaMP+=20;
		Opacity(H$("Footer"),60-opaMP*0.4);
		Opacity(H$("MusicPlayer"),opaMP);
		if(opaMP<100){
			tmrFadeMusicPlayer=window.setTimeout(fadeInMusicPlayer,100);
		}else{
			tmrFadeMusicPlayer=0;
			if (flagFlashMusicPlayer){
					tmrFadeMusicPlayer=window.setTimeout(fadeOutMusicPlayer,3000);
					flagFlashMusicPlayer=false;
			}
		}
		return;
	}
	
	function fadeOutMusicPlayer(){
		if(tmrFadeMusicPlayer!=0){
			window.clearTimeout(tmrFadeMusicPlayer);
			tmrFadeMusicPlayer=0;
		}
		opaMP-=20;
		Opacity(H$("Footer"),60-opaMP*0.4);
		Opacity(H$("MusicPlayer"),opaMP);
		if(opaMP>0){
			tmrFadeMusicPlayer=window.setTimeout(fadeOutMusicPlayer,100);
		}else{
			tmrFadeMusicPlayer=0;
		}
		return;
	}

	function ToolBarAnime(){
		opaTB+=20;
		Opacity(H$("LoginNotify"),opaTB);
		Opacity(H$("Loading"),100-opaTB);
		if(opaDLM<100){
			H$("LoginNotify").style.visibility="";
			window.setTimeout(ToolBarAnime,100);
		}else{
			H$("Loading").style.visibility="hidden";
		}
	}
	
	function GoRealMode(){
		getMode='real';
		if(tmrFetchInterval!=0) window.clearTimeout(tmrFetchInterval);
		GetTop5();
	}
	
	function GoBase(){
		getMode='base';
		if(tmrFetchInterval!=0) window.clearTimeout(tmrFetchInterval);
		GetTop5();
	}
	
	function GetTopNow(){
		if(getMode!="real") return;
		GetTopRefresh();
	}
	
	function GetTopRefresh(){
		lgStat="command";
		if(tmrFetchInterval!=0) window.clearTimeout(tmrFetchInterval);
		tmrFetchInterval=window.setTimeout(GetTop5,100);  //Refresh MessageList	
	}
	
	function GetTop5(){
		if(typeof xmlhttp=="object"){
			/*效果测试中*/
			if(xmlhttp.readyState!=4&&xmlhttp.readyState!=0) return;
			if(H$("Displayer").style.visibility=="")
				xmlhttp.open('post','5.mdb?inc=true&mode='+getMode,true);
			else
				xmlhttp.open('post','5.mdb?inc=false&mode='+getMode,true);
			xmlhttp.onreadystatechange = DataArrival;
			xmlhttp.send(null);
		}
	}
	
	function DataArrival(){
		if (xmlhttp.readyState == 4){
			if(xmlhttp.status == 200){
				try{
					var rtn = eval("("+xmlhttp.responseText+")");
				}catch(e){
					try{
						var rtn = JSON.parse(xmlhttp.responseText);
					}catch(e){}
				}
				if(rtn["Mode"]=="real")
					tmrFetchInterval=window.setTimeout(GetTop5,5000);  //Refresh MessageList
				if(rtn["Session"]!=lgStat){
					//H$("fraLoginNotify").src="./Top.asp?nocache=" + Math.random();
					if(H$("divContent").style.visibility!=""){
	        		 if(IsIELEQ8()) H$("LoginNotify").innerHTML=("<a href='#' onclick='window.alert(\'You Have Got A IE kernel versioned 5.5-7.\nSorry That I can't completely fix the problem currently.\nYou may try some W3C based webbrowsers ex. Firefox, Chrome...\');'><font color=red> Sorry, Old IE ! </font></a>");
				    else H$("LoginNotify").innerHTML="";
				    if (rtn["Session"]!="")
					H$("LoginNotify").innerHTML=H$("LoginNotify").innerHTML + 'Welcome,' + rtn["Session"] + ' You can [  <a href="#" onclick="gotoLocation(\'PostMessage.asp\');">Post</a>  |  <a href="#" onclick="gotoLocation(\'LogOut.asp\');">Logout</a>  ] <a href="#" onclick="gotoLocation(\'help.html\');">HELP!</a>';
					else
				    H$("LoginNotify").innerHTML=H$("LoginNotify").innerHTML + '<a href="#" onclick="gotoLocation(\'Login.asp\');">Login</a> or <a href="#" onclick="gotoLocation(\'Register.asp\');">Register</a> to have a Post! <a href="#" onclick="gotoLocation(\'help.html\');"><font color=red>HELP!</font></a>';
				  }
				}
				
				getMode=rtn["Mode"]
				if(rtn["Mode"]=="before"){
					SlideDirectionChange(1);
				}else{
					SlideDirectionChange(0);
				}
				
				lgStat=rtn["Session"];
				
				if(rtn["Result"]!=null){
					for(var n in rtn["Result"]){
						SliderBoot(
							"<div style='position:absolute; top:0px; left:74px; height:20%; color:#C06; font-family:微软雅黑; font-size:12px;'>Author: "+rtn["Result"][n]["Author"]+"</div>"+
                        		"<div style='position:absolute; top:0px; right:5px ; height:20%; text-align:right; color:#C06; font-family:Comic Sans MS, cursive; font-size:12px;'> At:"+rtn["Result"][n]["TOB"]+"</div>"+
                        		"<div style='position:absolute; bottom:0px; left:74px; right:5px; height:80%; width:85%; text-align:left; font-family:微软雅黑; '>"+rtn["Result"][n]["Message"]+"</div>"+
                            	"<div style='position:absolute; left:6px; top:50%; margin-top:-32px; width:64px; height:64px'>"+
                        		"<div id='outer'><div id='middle'><img id='inner' src='"+rtn["Result"][n]["Avatar"]+"' width='64' height='64' onload='ImgAuto(this,64,64);' /></div></div>"+
							"</div>");
						try{
							if(rtn["ActiveUsers"]>1)
								if(rtn["ActiveUsers"]==1)
									H$("State").innerHTML='You are the only user online. Let your friends know here!';
								else if(rtn["ActiveUsers"]==2)
										H$("State").innerHTML='Fatal Error: You are with only one now!';
									else
										H$("State").innerHTML='You can talk with '+ (rtn["ActiveUsers"]-1) +' users online.';
							else
								H$("State").innerHTML='You can talk with peoples online. Invite them!';
						}catch(e){}
					}
				}
				httpFailedTry=0;
			}else{
				httpFailedTry++;
				if(httpFailedTry==5)
					alert("Netwrk Error! Cannot Reach Message Server. Please Refresh This Page!");
				else
					window.setTimeout(GetTop5,5000);
			}
		}
	}
	
	function fadeIn(){
		if(tmrFade!=0){
			window.clearTimeout(tmrFade);
			tmrFade=0;
		}
		opaDLM+=20;
		Opacity(H$("Displayer"),60-opaDLM*0.4);
		Opacity(H$("divContent"),opaDLM);
		if(opaDLM<100){
			H$("divContent").style.visibility="";
			H$("LoginNotify").innerHTML = '<a href="#" onclick="fadeOut();">&gt;CLOSE WINDOW&lt;</a>';
			tmrFade=window.setTimeout(fadeIn,100);
		}else{
			tmrFade=0;
		}
		return;
	}
	
	function fadeOut(){
		if(tmrFade!=0){
			window.clearTimeout(tmrFade);
			tmrFade=0;
		}
		opaDLM-=20;
		Opacity(H$("Displayer"),60-opaDLM*0.4);
		Opacity(H$("divContent"),opaDLM);

		if(opaDLM>0){
			H$("divContent").style.visibility="";
			tmrFade=window.setTimeout(fadeOut,100);
		}else{
			H$("divContent").style.visibility="hidden";
			tmrFade=window.setTimeout(gotoLocationBlank,100);
			getMode="session";
			GetTopRefresh();
		}
		return;
	}
	
	function getItem(index){
		return window.document.getElementById("divItem"+index.toString());
	}
	
	function SliderBoot(m_html){
		SlideHTMLSet.push(m_html);
		SlideCount++;
		if(SlideCount==1) window.setTimeout(Slider,100);
	}
	
	function InitOrder(){
		opaItem=100;
		SlideCount=0;
		lastOrder=0;
		var objLO=getItem(4);
		Opacity(objLO,0);
		if(SlideDirection==0)
			objLO.style.top="0%";
		else
			objLO.style.top="75%";
		objLO.style.left="100px";
		for(i=0;i!=4;i++){
			var objOth=getItem(i);
			Opacity(objOth,0);
			objOth.style.visibility="hidden";
		}
	}
	
	function NextOrder(){
		if(SlideDirection==0) ChangeLastOrder(-1);
		else ChangeLastOrder(1);
		var objLO=getItem(lastOrder);
		opaItem=-150;
		Opacity(objLO,0);
		if(SlideDirection==0)
			objLO.style.top="0%";
		else
			objLO.style.top="75%";
		objLO.style.left="100px";
		objLO.style.visibility="";
		objLO.children[4].innerHTML=SlideHTMLSet.pop();
		//TODO: Execute Script In Message
	}
	
	function SlideDirectionChange(dir){
		if(dir==SlideDirection) return;
		if(dir==1){
			SlideDirection=1;
			ChangeLastOrder(-2);
		}else{
			SlideDirection=0;
			ChangeLastOrder(2);
		}
	}
	
	function Slider(){
		var tmr=window.setTimeout(Slider,15);
		if(opaItem==100){
			NextOrder();
		}
		for(i=0;i!=5;i++){
			if(lastOrder!=i){
				var itm=getItem(i);
				if(SlideDirection==0){
					if(parseInt(itm.style.top)>75)
						Opacity(itm,100-(parseInt(itm.style.top)-75)/20*100);
					itm.style.top=(parseInt(itm.style.top)+1) + "%";
				}else{
					if(parseInt(itm.style.top)<0)
						Opacity(itm,100+(parseInt(itm.style.top))/20*100);
					itm.style.top=(parseInt(itm.style.top)-1) + "%";
				}
			}
		}
		var itm=getItem(lastOrder);
		opaItem+=10;
		if(opaItem>=0) Opacity(itm,opaItem);
		itm.style.left=((Math.pow((opaItem-100),2)+(opaItem-100))/(-99*5))+"px";
		if(opaItem==100){
			SlideCount--;
			if(SlideCount<=0)
				window.clearTimeout(tmr);
			if(SlideDirection==0){
				ChangeLastOrder(-1);
				var objLO=getItem(lastOrder);
				objLO.style.top="0%";
				objLO.style.left="100px";
				objLO.style.visibility="hidden";
				ChangeLastOrder(1);
			}
		}
	}
	
	function ChangeLastOrder(inc){
		lastOrder+=inc;
		while(lastOrder<0) lastOrder+=5;
		lastOrder%=5;
	}
	
	function gotoLocationBlank(){
		H$("fraContent").src="about:blank";
		tmrFade=0;	
	}
	
	function gotoLocation(loc){
		if(H$("fraContent").src!="about:blank") fadeOut();
		if((H$("fraContent").src).indexOf(loc)<0)
			if(loc.indexOf("?")<0)
				H$("fraContent").src=loc+"?nocache=" + Math.random();
			else
				H$("fraContent").src=loc;
		//fadeIn();
	}
	

	

